﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BookStats
{
    class Order
    {
        Dictionary<int, HashSet<int>> orders;

        public Order(String path)
        {
            orders = GetOrders(path);
        }

        private Dictionary<int, HashSet<int>> GetOrders(string path)
        {
            Dictionary<int, HashSet<int>> items = new Dictionary<int, HashSet<int>>();
            using (StreamReader sr = File.OpenText(path))
            {
                string s = String.Empty;
                while ((s = sr.ReadLine()) != null)
                {
                    string[] line = s.Split(',');
                    try
                    {
                        int customerId = int.Parse(line[0]);
                        int bookId = int.Parse(line[1]);
                        if (items.ContainsKey(bookId))
                        {
                            items[bookId].Add(customerId);
                        }
                        else
                        {
                            HashSet<int> customers = new HashSet<int>();
                            customers.Add(customerId);
                            items.Add(bookId, customers);
                        }
                    }
                    catch{ }
                }
            }
            return items;
        }

        public int GetOrderCount(string x, string y){
            int count = 0;
            foreach (KeyValuePair<int,HashSet<int>> order in orders)
            {
                var receipt = order.Value;
                if (receipt.Contains(x) && receipt.Contains(y))
                {
                    count++;
                }
            }
            return count;
        }

        public void GetAllPairs()
        {
            Stopwatch watch = new Stopwatch();
            watch.Start();
            for (int i = 0; i < books.Count; i++)
            {
                for (int j = i+1; j < books.Count;j++)
                {
                    int count = GetOrderCount(books[i], books[j]);

                }
                Console.WriteLine(watch.Elapsed);
            }
            watch.Stop();
        }

        public void GetAllPercents()
        {
            foreach (KeyValuePair<int, HashSet<int>> order in orders)
            {
                var receipt = order.Value;
                foreach (int i in receipt)
                {
                    if (receipt.Contains(i))
                    {
                        for(int)
                    }
                }
            }
        }

        public int GetBookCount() {
            return books.Count;
        }

        public void GetCustomerPurchase(int id)
        {
            foreach (string s in orders[id])
            {
                System.Console.WriteLine("Raamat " + s);
            }
        }


        
    }
}
